package com.localjoycommunity.cpdj.project.system.controller;

import com.localjoycommunity.cpdj.common.R;
import com.localjoycommunity.cpdj.common.interceptor.help.NeedLogin;
import com.localjoycommunity.cpdj.common.utils.poi.ExcelUtil;
import com.localjoycommunity.cpdj.framework.aspectj.lang.annotation.Log;
import com.localjoycommunity.cpdj.framework.aspectj.lang.enums.BusinessType;
import com.localjoycommunity.cpdj.framework.web.controller.BaseController;
import com.localjoycommunity.cpdj.framework.web.domain.AjaxResult;
import com.localjoycommunity.cpdj.framework.web.page.TableDataInfo;
import com.localjoycommunity.cpdj.project.system.domain.SysPaper;
import com.localjoycommunity.cpdj.project.system.domain.SysPaperQuestion;
import com.localjoycommunity.cpdj.project.system.domain.UserAnswer;
import com.localjoycommunity.cpdj.project.system.domain.vo.QuestionPaperVo;
import com.localjoycommunity.cpdj.project.system.domain.vo.QuestionVo;
import com.localjoycommunity.cpdj.project.system.service.ISysPaperQuestionService;
import com.localjoycommunity.cpdj.project.system.service.ISysPaperService;
import com.localjoycommunity.cpdj.project.system.service.ISysQuestionService;
import com.localjoycommunity.cpdj.project.system.service.IUserAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 试卷Controller
 *
 * @author leepandar
 * @date 2020-11-01
 */
@RestController
@RequestMapping("/system/paper")
public class SysPaperController extends BaseController {
    @Autowired
    private ISysPaperService sysPaperService;

    @Autowired
    private ISysQuestionService sysQuestionService;

    @Autowired
    private ISysPaperQuestionService sysPaperQuestionService;

    @Autowired
    private IUserAnswerService userAnswerService;

    /**
     * 查询试卷列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysPaper sysPaper) {
        startPage();
        List<SysPaper> list = sysPaperService.selectSysPaperList(sysPaper);
        return getDataTable(list);
    }

    @GetMapping("/app/list")
    @NeedLogin
    public R<List<SysPaper>> appList(SysPaper sysPaper, HttpServletRequest req) {
        Integer userId = getUserId(req);
        List<SysPaper> list = sysPaperService.selectSysPaperList(sysPaper);
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setUserId(Long.valueOf(userId));
        userAnswer.setPaperId(sysPaper.getPaperId());
        List<UserAnswer> userPaperList = userAnswerService.selectUserAnswerList(userAnswer);
        Set<Long> set = userPaperList.stream().map(UserAnswer::getPaperId).collect(Collectors.toSet());
        list.forEach(item -> {
            if(set.contains(item.getPaperId())){
                item.setIsDone(1);
            }
        });
        return R.success(list);
    }

    /**
     * 导出试卷列表
     */
    @Log(title = "试卷", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysPaper sysPaper) {
        List<SysPaper> list = sysPaperService.selectSysPaperList(sysPaper);
        ExcelUtil<SysPaper> util = new ExcelUtil<SysPaper>(SysPaper.class);
        return util.exportExcel(list, "paper");
    }

    /**
     * 获取试卷详细信息
     */
    @GetMapping(value = "/{paperId}")
    public AjaxResult getInfo(@PathVariable("paperId") Long paperId) {
        SysPaper sysPaper = sysPaperService.selectSysPaperById(paperId);
        SysPaperQuestion sysPaperQuestion = new SysPaperQuestion();
        sysPaperQuestion.setPaperId(paperId);
        List<QuestionPaperVo> sysPaperQuestions = sysPaperQuestionService.selectSysPaperQuestionList(paperId);
        sysPaper.setPaperQuestions2(sysPaperQuestions);
        return AjaxResult.success(sysPaper);
    }

    /**
     * app 根据试卷id 查询试题列表
     */
    @GetMapping(value = "/app/{paperId}")
    public R<List<QuestionVo>> getQusetionsByPaperId(@PathVariable("paperId") Long paperId){
        List<QuestionVo> questionVos = sysQuestionService.selectQuestions(paperId);
        questionVos.forEach(item-> item.setOptions(sysQuestionService.selectOptions(item.getQuestionId())));
        return R.success(questionVos);
    }

    /**
     * 新增试卷
     */
    @Log(title = "试卷", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Long> add(@RequestBody SysPaper sysPaper) {
        long i = sysPaperService.insertSysPaper(sysPaper);
        List<SysPaperQuestion> paperQuestions = sysPaper.getPaperQuestions();
        paperQuestions.forEach(item -> {
            item.setPaperId(i);
        });
        paperQuestions.forEach(item -> {
            // 新增试题
            sysPaperQuestionService.insertSysPaperQuestion(item);
        });
        return R.success(i);
    }

    /**
     * 修改试卷
     */
    @Log(title = "试卷", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysPaper sysPaper) {
        // 修改试卷
        int i = sysPaperService.updateSysPaper(sysPaper);
        List<SysPaperQuestion> paperQuestions = sysPaper.getPaperQuestions();
        if(paperQuestions != null && paperQuestions.size() > 0){
            // 删除所有试题
            sysPaperQuestionService.deleteSysPaperQuestionById(sysPaper.getPaperId());
            paperQuestions.forEach(item -> {
                // 修改试卷试题
                sysPaperQuestionService.insertSysPaperQuestion(item);
            });
        }
        return toAjax(i);
    }

    /**
     * 删除试卷
     */
    @Log(title = "试卷", businessType = BusinessType.DELETE)
    @DeleteMapping("/{paperIds}")
    public AjaxResult remove(@PathVariable Long[] paperIds) {
        return toAjax(sysPaperService.deleteSysPaperByIds(paperIds));
    }
}
